#include "../DataStructures/pq.h"

typedef struct _node {
    int key;
    int id ;
} node;

int key_cmp(const void *a, const void *b) {
    if (((node *) a)->key > ((node *) b)->key) return 1;
    else if (((node *) a)->key < ((node *) b)->key) return -1;

    return 0;
}

void data_copy(void *dest, const void *src) {
    node *n = dest;
    ((node *) dest)->key = ((node *) src)->key;
    ((node *) dest)->id = ((node *) src)->id;
}

void del_node(void *n) {
    printf("deleting id [%d] node [%d] \n", ((node *) n)->id, ((node *) n)->key);
    return;
}

void print_node(const void *n) {
    printf("id [%d] node [%d] \n", ((node *) n)->id, ((node *) n)->key);
    return;
}

void pq_driver() {
    int k, cases, id, sid;
    node n;
    PQ pq;

#ifndef ONLINE_JUDGE
    freopen("pq.in", "r", stdin);
#endif

    scanf("%d", &cases);
    while (cases--) {
        pq_init(&pq, 100, sizeof (node), key_cmp, data_copy, del_node, print_node);
        scanf("%d", &k);
        id=1 ;
        while (k--) {
            scanf("%d", &n.key);
            n.id = id++ ;
            pq_enqueue(&pq, &n, n.id);
        }

        id = pq_peek(&pq, &n);
        printf("(%d) peek=%d\n", id, n.key);

        id = pq_dequeue(&pq, &n);
        printf("(%d) dequeue=%d id=%d\n", id, n.key, n.id);
        sid = id ;

        id = pq_peek(&pq, &n);
        printf("(%d) peek=%d\n", id, n.key);

        n.key = 0;
        n.id = sid ;
        pq_enqueue(&pq, &n, sid);

        n.key = -1;
        id = pq_peek(&pq, &n);
        if (n.key == -1)
            printf("(%d) queue empty\n", id);
        else
            printf("(%d) peek=%d\n", id, n.key);

        pq_delete(&pq);
        
        id = pq_peek(&pq, &n);
        if (id == 0)
            printf("(%d) queue empty\n", id);
        else
            printf("(%d) peek last %d\n", id, n.key);
    }

}
